<template>
  <div class="app-container">
    <div class="block">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-date-picker
            v-model="rangeDate"
            type="datetimerange"
            range-separator="至"
            start-placeholder="发送起始日期"
            end-placeholder="发送截至日期"
            value-format="yyyyMMddHHmmss"
            align="right"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="6">
          <el-button type="success" icon="el-icon-search" @click.native="search">{{
            $t('button.search')
          }}</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click.native="reset">{{
            $t('button.reset')
          }}</el-button>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col :span="24">
          <el-button type="danger" icon="el-icon-delete" @click.native="clear">{{
            $t('button.clear')
          }}</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="模板编码">
        <template slot-scope="scope">
          {{ scope.row.tplCode }}
        </template>
      </el-table-column>
      <el-table-column label="消息内容">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="接收者">
        <template slot-scope="scope">
          {{ scope.row.receiver }}
        </template>
      </el-table-column>
      <el-table-column label="发送日期">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="消息类型">
        <template slot-scope="scope">
          {{ scope.row.type == 0 ? '短信' : '邮件' }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.state == 1 ? '成功' : '失败' }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100, 500]"
      :page-size="listQuery.limit"
      :total="total"
      @size-change="changeSize"
      @current-change="fetchPage"
      @prev-click="fetchPrev"
      @next-click="fetchNext"
    >
    </el-pagination>
  </div>
</template>

<script>
import { clear, getList } from '@/api/message/message';

export default {
  data() {
    return {
      formVisible: false,
      formTitle: '添加历史消息',
      isAdd: true,
      form: {
        tplCode: '',
        content: '',
        receiver: '',
        type: '',
        id: '',
      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
      },
      rangeDate: undefined,
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
    };
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger',
      };
      return statusMap[status];
    },
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      let queryData = this.listQuery;
      if (this.rangeDate) {
        queryData['startDate'] = this.rangeDate[0];
        queryData['endDate'] = this.rangeDate[1];
      }
      getList(queryData).then((response) => {
        this.list = response.data.records;
        this.listLoading = false;
        this.total = response.data.total;
      });
    },
    search() {
      this.fetchData();
    },
    reset() {
      this.listQuery.startDate = undefined;
      this.listQuery.endDate = undefined;
      this.rangeDate = '';
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.page = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.limit = limit;
      this.fetchData();
    },
    clear() {
      this.$confirm('确认清楚所有历史消息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        clear().then(() => {
          this.fetchData();
          this.$message({
            type: 'success',
            message: '清楚成功!',
          });
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import '~@/styles/common.scss';
</style>
